package com.bayss.core.service.code;

import com.bayss.core.entity.code.CodeBase;
import com.bayss.core.entity.code.CodeBaseExample;
import com.bayss.core.entity.code.mapper.CodeBaseMapper;
import com.bayss.core.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author pangxin1
 * @date 2019-11-10 13:17
 */
@Service
public class CodeBaseService {


    @Autowired
    private CodeBaseMapper codeBaseMapper;


    /**
     * 获得自选餐配置规则
     *
     * @return
     */
    public String getCodeValueByCode(String code) {
        CodeBaseExample codeBaseExample = new CodeBaseExample();
        CodeBaseExample.Criteria criteria = codeBaseExample.createCriteria();
        criteria.andObgKeyEqualTo(code);
        List<CodeBase> codeBases = codeBaseMapper.selectByExample(codeBaseExample);
        if (CollectionUtils.isNotEmpty(codeBases)) {
            return codeBases.get(0).getObgValue();
        }
        return "";
    }

    /**
     * 获得自选餐配置规则
     *
     * @return
     */
    public CodeBase getCodeEntityByCode(String code) {
        CodeBaseExample codeBaseExample = new CodeBaseExample();
        CodeBaseExample.Criteria criteria = codeBaseExample.createCriteria();
        criteria.andObgKeyEqualTo(code);
        List<CodeBase> codeBases = codeBaseMapper.selectByExample(codeBaseExample);
        if (CollectionUtils.isNotEmpty(codeBases)) {
            return codeBases.get(0);
        }
        return null;
    }

    public List<CodeBase> getCodesValueByCode(String code) {
        CodeBaseExample codeBaseExample = new CodeBaseExample();
        CodeBaseExample.Criteria criteria = codeBaseExample.createCriteria();
        criteria.andObgKeyEqualTo(code);
        List<CodeBase> codeBases = codeBaseMapper.selectByExample(codeBaseExample);
        return codeBases;
    }

    public boolean add(CodeBase codeBase) {
        codeBase.setCreateTime(new Date());
        return codeBaseMapper.insertSelective(codeBase) > 0;
    }

    public boolean del(int id) {
        return codeBaseMapper.deleteByPrimaryKey(id) > 0;
    }
}
